package org.dk.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import org.dk.domain.DataResult;
import org.dk.em.DataResultEnum;
import org.dk.pageInfo.PageInfo2;
import org.dk.pojo.TbItemCat;
import org.dk.pojo.TbTypeTemplate;
import org.dk.service.TbItemCatService;
import org.dk.service.TbTypeTemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("tbItemCatController")
public class TbItemCatController {

    @Reference
    private TbItemCatService tbItemCatService;
    @Reference
    private TbTypeTemplateService temService;


    @RequestMapping("toTbItemCatShow")
    public String ToTbItemCatShow(){
        return "admin/item_cat";
    };

    @RequestMapping("selectItemCat")
    @ResponseBody
    public DataResult selectItemCat(@RequestParam(defaultValue = "1")Integer pageNum,
                                    @RequestParam(defaultValue = "5") Integer pageSize, Long parentId){
        PageInfo2 page = tbItemCatService.select(pageNum,pageSize,parentId);
        Map<String,Object> map = new HashMap<>();
        map.put("page",page);
        if(parentId != 0){
            TbItemCat itemCat = tbItemCatService.selectById(parentId);
            map.put("itemcat",itemCat);
        }
        return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(map);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public DataResult selectAll(){
            List<TbTypeTemplate> typeTemplates = temService.selectAll();
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(typeTemplates);
    }

    @RequestMapping("addItemCat")
    @ResponseBody
    public DataResult addItemCat(TbItemCat itemCat){
        tbItemCatService.addItemCat(itemCat);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS);
    }

    @RequestMapping("deleteItemType")
    @ResponseBody
    public DataResult deleteItemType(@RequestParam("ids[]") Long[] ids){
        tbItemCatService.deleteItemType(ids);
        return DataResult.buildDataResult(DataResultEnum.SUCCESS);
    }

    @RequestMapping("updateItemCatReturn")
    @ResponseBody
    public DataResult updateItemCatReturn(Long id){
        TbItemCat itemCat = tbItemCatService.updateItemCatReturn(id);
        List<TbTypeTemplate> typeTemplates = temService.selectAll();
        Map<String,Object> map = new HashMap<>();
        map.put("itemCat",itemCat);
        map.put("typeTem",typeTemplates);
        return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(map);
    }
    @RequestMapping("updateItemType")
    @ResponseBody
    public DataResult updateItemType(TbItemCat itemCat){
        tbItemCatService.updateItemType(itemCat);
        return DataResult.buildDataResult(DataResultEnum.SUCCESS);
    }
}
